package com.kylin.module.study.business;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.util.FileOperater;
import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.module.study.forms.VocabularyForm;
import com.kylin.module.study.orm.Vocabulary;
import com.kylin.module.study.orm.Vocabularyerrors;
import com.kylin.module.user.orm.User;

/**
 * 业务逻辑类：处理词汇相关的业务功能
 * @author ly
 *
 */
public class VocabularyBlogic {
	protected static Logger log = Logger.getLogger(VocabularyBlogic.class);
	
	/**
	 * 添加新词汇 
	 * @param form
	 */
	public void addVocabulary(VocabularyForm form) throws BusinessException {
		log.debug("插入新的词汇 ：" + form.getContent());
		Vocabulary same = new Vocabulary();
		same.setLanguage(form.getLanguage());
		same.setContent(form.getContent());
		same.setTargetlang(form.getTargetlang());
		List sames = BaseHibernateDAO.findByExample(same);
		if(sames != null && sames.size() > 0){
			throw new BusinessException ("已经存在相同的词汇及语种的信息");
		}
		System.out.println("");
		Vocabulary obj = new Vocabulary();
		obj.setActive(new Short("1"));
		//上传声音文件
		if(form.getAudio()!=null && form.getAudio().getFileSize() > 0){
			String audioPath = StudyBlogic.uploadMedia(Constants.MEDIA_VOCABUL, form.getAudio(), null);
		    obj.setAudio(audioPath);
		}
		obj.setCatvocabulary(new Dict(form.getCatvocabularyid()));
		obj.setCreatedon(new Date());
		obj.setLastupdatedon(new Date());
		obj.setLanguage(form.getLanguage());
		obj.setTargetlang(form.getTargetlang());
		obj.setOrdering(form.getOrdering());
		obj.setContent(form.getContent());
		obj.setTranslation(form.getTranslation());
		obj.setPhonetic(form.getPhonetic());
		BaseHibernateDAO.save(obj);
	}
	
	/**
	 * 修改学习词汇
	 * 
	 * @param form
	 */
	public void updateVocabulary(VocabularyForm form) throws BusinessException {
		log.debug("修改词汇 ：" + form.getContent());
		Vocabulary obj = (Vocabulary) BaseHibernateDAO.findById(Vocabulary.class, form.getId());
		//上传 修改 声音文件
		if(form.getAudio()!=null || form.getAudio().getFileSize() > 0){
			String audioPath = StudyBlogic.uploadMedia(Constants.MEDIA_VOCABUL, form.getAudio(), obj.getAudio());
		    obj.setAudio(audioPath);
		}
		obj.setCatvocabulary(new Dict(form.getCatvocabularyid()));
		obj.setLastupdatedon(new Date());//最后修改时间
		obj.setContent(form.getContent());
		obj.setTranslation(form.getTranslation());
		BaseHibernateDAO.update(obj);
	}
	
	/**
	 * 根据ID删除词汇
	 * 
	 * @param id
	 */
	public void deleteById(Long id) throws BusinessException {
		Vocabulary obj = getVocabularyById(id);
		if (obj != null) {
			BaseHibernateDAO.delete(obj);
			try {
				FileOperater.deleteFile(Constants.MEDIA_ARTICLE, obj.getAudio());
			} catch (IOException e) {
				e.printStackTrace();
				throw new BusinessException("删除词汇附属声音文件失败："+Constants.MEDIA_ARTICLE +File.separator+ obj.getAudio());
			}
		}
	}
	/**
	 * 根据ID获得词汇
	 * 
	 * @param id
	 */
	public Vocabulary getVocabularyById(Long id) throws BusinessException {
		return (Vocabulary) BaseHibernateDAO.findById(Vocabulary.class, id);
	}
	/**
	 * 根据ID删除词汇报错信息
	 * 
	 * @param id
	 */
	public void deleteErrorById(Long id) throws BusinessException {
		Vocabularyerrors obj = getVocabularyErrorById(id);
		if (obj != null) {
			BaseHibernateDAO.delete(obj);
		}
	}
	/**
	 * 根据ID获得报错词汇
	 * 
	 * @param id
	 */
	public Vocabularyerrors getVocabularyErrorById(Long id) throws BusinessException {
		return (Vocabularyerrors) BaseHibernateDAO.findById(Vocabularyerrors.class, id);
	}
	/**
	 * 插入新的错误词汇信息
	 * 
	 * @param user
	 * @param form
	 */
	public void addVocabularyError(User user,Vocabulary vocabulary,String description) throws BusinessException
	{
		Vocabularyerrors obj = new Vocabularyerrors();
		obj.setCreatedon(new Date());
		obj.setDescription(description);
		obj.setLanguage(vocabulary.getLanguage());
		obj.setUser(user);
		obj.setVocabulary(vocabulary);
		BaseHibernateDAO.save(obj);
	}
}
